package com.task.controller;

import java.util.Map;

import org.quartz.Trigger.TriggerState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.task.bean.TaskRunBean;
import com.task.quartz.JobUtil;
import com.task.services.TaskRunService;
import com.task.util.StringUtil;

@Controller
@RequestMapping(value="jobManager")
public class JobController {
	    @Autowired
	    private JobUtil jobUtil;
	    
	    @Autowired
	    private TaskRunService taskRunService;
	    
	    @RequestMapping(value="/addJob",method=RequestMethod.POST)
		@ResponseBody
		public  Map<String,Object> addTaskRun(@RequestParam("id") int id) throws Exception {
	    	TaskRunBean tb=this.taskRunService.getTaskRunBean(id);
	    	if(null==tb) {
	    		return StringUtil.returnMap("F", "任务不存在!");
	    	}
	    	
	    	if(!"1".equals(tb.getStatus())) {
	    		tb.setStatus("1");
	    		taskRunService.updateTaskRun(tb);
	    	}
	    	
	    	TriggerState status=jobUtil.getJobState(tb.getJobName(), tb.getJobGroup());
	    	if(TriggerState.NORMAL.equals(status) ||
	    	   TriggerState.COMPLETE.equals(status) ||
	    	   TriggerState.BLOCKED.equals(status)) {
	    		return StringUtil.returnSucess();
	    	}else if(TriggerState.PAUSED.equals(status)) {
	    		return jobUtil.resumeJob(tb.getJobName(), tb.getJobGroup());
	    	}else {
	    		return jobUtil.addJob(tb);
	    	}
		}
	    
	    
	    @RequestMapping(value="/pauseJob",method=RequestMethod.POST)
		@ResponseBody
		public  Map<String,Object> pauseJob(@RequestParam("id") int id) throws Exception {
	    	TaskRunBean tb=this.taskRunService.getTaskRunBean(id);
	    	if(null==tb) {
	    		return StringUtil.returnMap("F", "任务不存在!");
	    	}
	    	
	    	if(TriggerState.NONE.equals(jobUtil.getJobState(tb.getJobName(), tb.getJobGroup()))) {
	    		return StringUtil.returnMap("F", "任务未执行!");
	    	}
	    	
	    	if(!"2".equals(tb.getStatus())) {
	    		tb.setStatus("2");
	    	    taskRunService.updateTaskRun(tb);
	    	}
	    	return jobUtil.pauseJob(tb.getJobName(), tb.getJobGroup());
		}
	    
	    
	    @RequestMapping(value="/deleteJob",method=RequestMethod.POST)
		@ResponseBody
		public  Map<String,Object> deleteJob(@RequestParam("id") int id) throws Exception {
	    	TaskRunBean tb=this.taskRunService.getTaskRunBean(id);
	    	if(null==tb) {
	    		return StringUtil.returnMap("F", "任务不存在!");
	    	}
	    	
	    	if(!"0".equals(tb.getStatus())) {
	    		tb.setStatus("0");
	    		taskRunService.updateTaskRun(tb);
	    	}
	    	
	    	return jobUtil.deleteJob(tb.getJobName(), tb.getJobGroup());
		}
	    
	    @RequestMapping(value="/runJob",method=RequestMethod.POST)
		@ResponseBody
		public  Map<String,Object> runJob(@RequestParam("id") int id) throws Exception {
	    	TaskRunBean tb=this.taskRunService.getTaskRunBean(id);
	    	if(null==tb) {
	    		return StringUtil.returnMap("F", "任务不存在!");
	    	}
	    	
	    	if(TriggerState.NONE.equals(jobUtil.getJobState(tb.getJobName(), tb.getJobGroup()))) {
	    		return StringUtil.returnMap("F", "任务未执行!");
	    	}
	    	
	    	jobUtil.immediateRun(tb);
	    	
			return StringUtil.returnSucess();
		}
	    
	    
}
